#include "func.h"

int main(int argc, char *argv[]){
    // ./client 192.168.145.129 1568
    ARGS_CHECK(argc, 3);
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in severAddr;
    severAddr.sin_family = AF_INET;
    severAddr.sin_port = htons(atoi(argv[2]));
    severAddr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd, (struct sockaddr *)&severAddr, sizeof(severAddr));
    ERROR_CHECK(ret, -1, "connect");
    //userLogin(sockfd);
    while(1){
        printf("please enter the order:");
        char buf[1024] = {0};
        char buf1[1024] = {0};
        char buf2[1024] = {0};
        read(STDIN_FILENO,buf,1024);
        sscanf(buf,"%s %s",buf1,buf2);
        //cichu
        logrecord(buf);
        int ch = getcmd(buf1);
        //memset(buf,0,1024);
        //sprintf(buf,"%d %s",ch,buf2);
        order_t Order;
        Order.type = ch;
        strcpy(Order.bufc, buf2);
        send(sockfd, &Order, sizeof(Order), 0);
        switch(ch){
        case CD:     
        case PWD:
            memset(buf,0,1024);
            ret=recv(sockfd,buf,1024,0);
            if(ret<0) continue;
            puts(buf);
            break;
        case GETS:
            memset(buf,0,1024);
////////////////////////////////////////////////////
            ret=recvFile(sockfd,Order.bufc);
////////////////////////////////////////////////////
            if(ret<0) continue;
            break;
        case PUTS:
            memset(buf,0,1024);
            ret=putsFile(sockfd,Order.bufc);
            if(ret<0) continue;
            break;
        case LS:
            while(1){
                int length=0;
                ret=recv(sockfd,&length,4,0);
                if(ret<0) continue;
                if(length==-1){
                    break;
                }
                memset(buf,0,1024);
                ret=recv(sockfd,buf,length,0);
                if(ret<0) continue;
                puts(buf);
            }
            break;
        case REMOVE:
        case MKDIR:
        case RMDIR:
        default:
            break;
        }
    }
    close(sockfd);
    return 0;
}


